package jsyf.oa.web.system;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.MessageFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Maqiuqiu on 2018/7/21.
 */
@Api(value = "消息接口", description = "消息接口")
@Controller
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageFacade messageFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.messageFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = messageFacade.delById(jsonParam.getString("id"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("删除失败！"));
        }
    }

    @RequestMapping(value = "/getMsgByUserId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取消息详情",
            notes = "获取消息详情",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{receiverUserId:''}", dataType = "String", paramType = "body")
    public void getMsgByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String receiverUserId = jsonParams.getString("receiverUserId");
        String messageId = jsonParams.getString("messageId");
        List<JSONObject> records = this.messageFacade.getMsgByUserId(receiverUserId, messageId);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
    }

    @RequestMapping(value = "/setMessageReadFlag.json", method = RequestMethod.POST)
    public void setMessageReadFlag(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("records", messageFacade.setMessageReadFlag(jsonData));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));
    }

    @RequestMapping(value = "/getMessageForApp.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "APP获取消息信息",
            notes = "APP获取消息信息",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{}", dataType = "String", paramType = "body")
    public void getMessageForApp(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject result = messageFacade.getMessageForApp(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(result));
    }

    @RequestMapping(value = "/getMessageForSignIn.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "区域经理迟到/缺勤提醒",
            notes = "区域经理迟到/缺勤提醒",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getMessageForSignIn(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        Boolean result = messageFacade.getMessageForSignIn();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));
    }

    @RequestMapping(value = "/getMessageForDaQuSignIn.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "大区经理迟到/缺勤提醒",
            notes = "大区经理迟到/缺勤提醒",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getMessageForDaQuSignIn(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        Boolean result = messageFacade.getMessageForDaQuSignIn();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));
    }

    @RequestMapping(value = "/getMessageForSupportSignIn.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "总监/支持迟到/缺勤提醒",
            notes = "总监/支持迟到/缺勤提醒",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void getMessageForSupportSignIn(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        Boolean result = messageFacade.getMessageForSupportSignIn();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));
    }
}
